package com.example.mybatisplus.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.common.utls.SessionUtils;
import com.example.mybatisplus.model.dto.PageDto;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.example.mybatisplus.service.UserService;
import com.example.mybatisplus.model.domain.User;


/**
 * 前端控制器
 *
 * @author lxp
 * @version v1.0
 * @since 2021-12-25
 */
@RestController
@RequestMapping("/api")
public class UserController {

    private final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @RequestMapping("/login")

    public JsonResponse<User> login(User user) {
        //先查这个用户
        User login = userService.login(user);
        if (login != null) {
            //cun Session
            SessionUtils.saveLoginUser(login);
        }

        return JsonResponse.success(login);
    }

    @RequestMapping("/re")
    public JsonResponse regist(@RequestBody User user) {
        System.out.println(user);
        JsonResponse jsonResponse = userService.regist(user);
//        System.out.println(user);
//        User one =user.setId(1111L).setUserType(1);
//        userService.save(user);
//        System.out.println(jsonResponse);
        return jsonResponse;
    }

    @GetMapping("/list")
    public JsonResponse list(PageDto page ,User user) {
        Page<User> list = userService.listByPage(page,user);
        return JsonResponse.success(list);
    }

    @GetMapping("/deleteById")
    public JsonResponse deleteById(Long id) {
        userService.removeById(id);
        return JsonResponse.success(true);
    }

    @PostMapping("/updateById")
    public JsonResponse updateById(@RequestBody User user) {
        userService.updateById(user);
        return JsonResponse.success(true);
    }
}

